package com.bh.extend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.PinyinUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.extend.domain.JcCustomer;
import com.bh.extend.mapper.JcCustomerMapper;
import com.bh.extend.service.IJcCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 客户管理Service业务层处理
 *
 * @author slx
 * @date 2023-04-02
 */
@Service
public class JcCustomerServiceImpl extends ServiceImpl<JcCustomerMapper, JcCustomer> implements IJcCustomerService {
    @Autowired
    private JcCustomerMapper jcCustomerMapper;

    /**
     * 查询客户管理
     *
     * @param id 客户管理主键
     * @return 客户管理
     */
    @DataDictClass
    @Override
    public JcCustomer selectJcCustomerById(Long id) {
        JcCustomer customer =jcCustomerMapper.selectById(id);
        if (customer != null) {
            boolean flag = DateUtils.belongCalendar(new Date(), customer.getStartTime(), customer.getEndTime());
            if (flag) {
                customer.setDays(DateUtils.getDayDiffer(new Date(), customer.getEndTime()));
            } else {
                customer.setDays(0);
            }
        }

        return customer;
    }

    @Override
    public JcCustomer selectJcCustomerByName(String name) {
        JcCustomer customer = jcCustomerMapper.selectJcCustomerByName(name);
        if (customer != null) {
            boolean flag = DateUtils.belongCalendar(new Date(), customer.getStartTime(), customer.getEndTime());
            if (flag) {
                customer.setDays(DateUtils.getDayDiffer(new Date(), customer.getEndTime()));
            } else {
                customer.setDays(0);
            }
        }
        return customer;
    }

    @Override
    public JcCustomer selectJcCustomerByCode(String code) {
        return jcCustomerMapper.selectJcCustomerByCode(code);
    }

    /**
     * 查询客户管理列表
     *
     * @param jcCustomer 客户管理
     * @return 客户管理
     */
    @DataDictClass
    @Override
    public List<JcCustomer> selectJcCustomerList(JcCustomer jcCustomer) {
        jcCustomer.setJcOrganizationId(SecurityUtils.getOrgId());
        List<JcCustomer> customers = jcCustomerMapper.selectJcCustomerList(jcCustomer);
        for (JcCustomer customer : customers) {
            boolean flag = DateUtils.belongCalendar(new Date(), customer.getStartTime(), customer.getEndTime());
            if (flag) {
                customer.setDays(DateUtils.getDayDiffer(new Date(), customer.getEndTime()));
            } else {
                customer.setDays(0);
            }
        }
        return customers;
    }

    @Override
    public List<JcCustomer> searchJcCustomerList(String keyword) {
        keyword = keyword.toUpperCase();
        return jcCustomerMapper.searchJcCustomerList(keyword);
    }

    /**
     * 新增客户管理
     *
     * @param jcCustomer 客户管理
     * @return 结果
     */
    @Override
    public int insertJcCustomer(JcCustomer jcCustomer) {
        if (StringUtils.isEmpty(jcCustomer.getCode())) {
            jcCustomer.setCode(PinyinUtils.onlyFirstCharUpper(jcCustomer.getName()));
        }
        if(StringUtils.isEmpty(SecurityUtils.getOrgId())) {
            throw new ServiceException("请选择网点！");
        }
        jcCustomer.setJcOrganizationId(SecurityUtils.getOrgId());
        if (StringUtils.isEmpty(jcCustomer.getJcOrganizationIds())) {
            jcCustomer.setJcOrganizationIds(SecurityUtils.getOrgId() + "");
        } else {
            if (!jcCustomer.getJcOrganizationIds().contains(SecurityUtils.getOrgId()+ "")) {
                jcCustomer.setJcOrganizationIds(jcCustomer.getJcOrganizationIds() + "," + SecurityUtils.getOrgId());
            }

        }
        jcCustomer.setFirstUpper(PinyinUtils.onlyFirstCharUpper(jcCustomer.getName()));
        jcCustomer.setCreateTime(DateUtils.getNowDate());
        return jcCustomerMapper.insert(jcCustomer);
    }

    /**
     * 修改客户管理
     *
     * @param jcCustomer 客户管理
     * @return 结果
     */
    @Override
    public int updateJcCustomer(JcCustomer jcCustomer) {
        jcCustomer.setJcOrganizationId(SecurityUtils.getOrgId());
        if (StringUtils.isEmpty(jcCustomer.getJcOrganizationIds())) {
            jcCustomer.setJcOrganizationIds(SecurityUtils.getOrgId() + "");
        } else {
            if (!jcCustomer.getJcOrganizationIds().contains(SecurityUtils.getOrgId()+ "")) {
                jcCustomer.setJcOrganizationIds(jcCustomer.getJcOrganizationIds() + "," + SecurityUtils.getOrgId());
            }

        }
        jcCustomer.setFirstUpper(PinyinUtils.onlyFirstCharUpper(jcCustomer.getName()));
        return jcCustomerMapper.updateById(jcCustomer);
    }

    /**
     * 批量删除客户管理
     *
     * @param ids 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteJcCustomerByIds(Long[] ids) {
        return jcCustomerMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除客户管理信息
     *
     * @param id 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteJcCustomerById(Long id) {
        return jcCustomerMapper.deleteById(id);
    }

    @Override
    public int changeStatus(JcCustomer jcCustomer) {
        return jcCustomerMapper.updateById(jcCustomer);
    }
}
